using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using Kangaroo.Controls;
using Kangaroo.IDAL;
using Kangaroo.Model;
using Kangaroo.Shell.Utils;

namespace Kangaroo.Shell
{
    partial class Main
    {
        class HistoryView : ChartSubView
        {
            public HistoryView(Main mainWin, ChartView parentView)
                : base(mainWin, parentView)
            {
            }

            public override void Refresh()
            {
                if(ParentView.CurrentChannel == null)
                {
                    return;
                }
                // BUG: Not specifying the channel.
                DAL.Data.Signal.ListSignalsOfChannelAsync(ParentView.CurrentChannel).Then(
                    signals => MainWin.BeginInvoke((Action)delegate
                    {
                        try
                        {
                            MainWin.GenerateHistoricalTrandingChart(signals);
                            MainWin.OnDataConnectionSuccess();
                        }
                        catch (Exception ex)
                        {
                            MainWin.UIFailureHandler(ex);
                        }
                    }), MainWin.DataErrorHandler);
            }

            public override void Activate()
            {
                MainWin.picMap.Hide();
                MainWin.splitInfoChart.Show();
                MainWin.splitInfoChart.Dock = DockStyle.Fill;

                MainWin.timeTree.Hide();
                MainWin.eventList.Hide();
                MainWin.splitTrees.Panel2Collapsed = true;
                MainWin.splitTreeEvent.Panel2Collapsed = true;

                MainWin.characteristicChartsTableLayoutPanel.Hide();
                MainWin.historyTrendTableLayoutPanel.Show();
                MainWin.historyTrendTableLayoutPanel.Dock = DockStyle.Fill;

                ParentView.ChannelSelected += OnChannelSelected;
            }

            public override void Deactivate()
            {
                ParentView.ChannelSelected -= OnChannelSelected;
                base.Deactivate();
            }

            void OnChannelSelected(object sender, EventArgs<Model.ChannelInfo> e)
            {
                Refresh();
            }
        }
    }
}